<?php
namespace common\components;

use Yii;
use yii\base\Behavior;
use yii\web\Response;

class MyResponseBehavior extends Behavior
{
    private $_startTime;

    public function events()
    {
        return [
            Response::EVENT_BEFORE_SEND => 'beforeSend',
            Response::EVENT_AFTER_SEND => 'afterSend',
        ];
    }

    public function beforeSend($event)
    {
        $this->_startTime = microtime(true);
        $hasError = false;
        if ($hasError) {
            $event->handled = true;
        }
    }

    public function afterSend($event)
    {
        $time = microtime(true) - $this->_startTime;
        Yii::error("MyResponseBehavior spent $time second.");
    }
}